import { request } from 'umi';

/**
 * 统一API服务类
 * 提供美团业务相关的所有API调用
 */
export class MeituanApiService {
  /**
   * 统一错误处理
   */
  static handleError(error) {
    console.error('API调用错误:', error);
    throw error;
  }

  /**
   * 格式化时间参数
   */
  static formatTimeParams(params) {
    const formatted = { ...params };
    
    ['start_time', 'end_time'].forEach(key => {
      if (formatted[key]) {
        formatted[key] = new Date(formatted[key]).getTime();
      }
    });
    
    return formatted;
  }

  /**
   * 美团配置相关API
   */
  static async getMeituanConfigs(params = {}) {
    try {
      return await request('/api/v1/meituan', {
        method: 'GET',
        params,
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async getMeituanConfigDetail(id) {
    try {
      return await request(`/api/v1/meituan/${id}`, {
        method: 'GET',
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async createMeituanConfig(data) {
    try {
      return await request('/api/v1/meituan', {
        method: 'POST',
        data,
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async updateMeituanConfig(id, data) {
    try {
      return await request(`/api/v1/meituan/${id}`, {
        method: 'PUT',
        data,
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async deleteMeituanConfig(id) {
    try {
      return await request(`/api/v1/meituan/${id}`, {
        method: 'DELETE',
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async syncMeituanConfig(id) {
    try {
      return await request(`/api/v1/meituan/sync/${id}`, {
        method: 'POST',
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  /**
   * 美团团购活动相关API
   */
  static async getMeituanGroups(params = {}) {
    try {
      const formattedParams = this.formatTimeParams(params);
      return await request('/api/v1/meituan/group', {
        method: 'GET',
        params: formattedParams,
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async getMeituanGroupDetail(id) {
    try {
      return await request(`/api/v1/meituan/group/${id}`, {
        method: 'GET',
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async createMeituanGroup(data) {
    try {
      const formattedData = this.formatTimeParams(data);
      return await request('/api/v1/meituan/group', {
        method: 'POST',
        data: formattedData,
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async updateMeituanGroup(id, data) {
    try {
      const formattedData = this.formatTimeParams(data);
      return await request(`/api/v1/meituan/group/${id}`, {
        method: 'PUT',
        data: formattedData,
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async deleteMeituanGroup(id) {
    try {
      return await request(`/api/v1/meituan/group/${id}`, {
        method: 'DELETE',
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  /**
   * 美团团购订单相关API
   */
  static async getMeituanGroupOrders(params = {}) {
    try {
      const formattedParams = this.formatTimeParams(params);
      return await request('/api/v1/meituan/group/order', {
        method: 'GET',
        params: formattedParams,
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async getMeituanGroupOrderDetail(id) {
    try {
      return await request(`/api/v1/meituan/group/order/${id}`, {
        method: 'GET',
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async processMeituanGroupOrder(data) {
    try {
      return await request('/api/v1/meituan/group/order/process', {
        method: 'POST',
        data,
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async useMeituanGroupVoucher(id) {
    try {
      return await request(`/api/v1/meituan/group/order/use/${id}`, {
        method: 'POST',
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  static async syncMeituanGroupOrders(params = {}) {
    try {
      return await request('/api/v1/meituan/group/order/sync', {
        method: 'POST',
        data: params,
      });
    } catch (error) {
      this.handleError(error);
    }
  }

  /**
   * 统计数据相关API
   */
  static async getMeituanGroupOrderStatistics(storeId) {
    try {
      return await request('/api/v1/meituan/group/order/statistics', {
        method: 'GET',
        params: { store_id: storeId },
      });
    } catch (error) {
      this.handleError(error);
    }
  }
}

// 为了向后兼容，保留原有的导出方式
export const meituanApi = MeituanApiService;